<template>
  <!-- 放置一个弹层 用来编辑新增节点 -->
  <el-dialog
    :title="edit? '编辑权限点':'新增权限点'"
    :visible.sync="showDialog"
    @close="btnCancel"
  >
    <!-- 表单 -->
    <el-form
      ref="perForm"
      :model="formData"
      :rules="rules"
      label-width="120px"
    >
      <el-form-item
        label="权限名称"
        prop="name"
      >
        <el-input
          v-model="formData.name"
          style="width:90%"
        />
      </el-form-item>
      <el-form-item
        label="权限标识"
        prop="code"
      >
        <el-input
          v-model="formData.code"
          style="width:90%"
        />
      </el-form-item>
      <el-form-item label="权限描述">
        <el-input
          v-model="formData.description"
          style="width:90%"
        />
      </el-form-item>
      <el-form-item label="开启">
        <el-switch
          v-model="formData.enVisible"
          active-value="1"
          inactive-value="0"
        />
      </el-form-item>
    </el-form>
    <el-row
      slot="footer"
      type="flex"
      justify="center"
    >
      <el-col :span="6">
        <el-button
          size="small"
          type="primary"
          @click="enterFn"
        >确定</el-button>
        <el-button
          size="small"
          @click="CancelFn"
        >取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
export default {
  data() {
    // 自定义 name 校验
    const validName = (rule, value, callback) => {
      let existNameList

      if (this.edit) {
        // 编辑时的校验
        existNameList = this.permissionList.filter(
          (item) =>
            item.pid === this.formData.pid && item.id !== this.formData.id
        )
      } else {
        // 添加时的校验
        existNameList = this.permissionList.filter(
          (item) => item.pid === this.formData.pid
        )
      }
      existNameList.map((item) => item.name).includes(value)
        ? callback(new Error(value + '已经占用'))
        : callback()
    }

    // 自定义 code 校验
    const validCode = (rule, value, callback) => {
      let existNameList

      if (this.edit) {
        // 编辑时的校验
        existNameList = this.permissionList.filter(
          (item) => item.id === this.formData.id
        )
      } else {
        // 添加时的校验
        existNameList = this.permissionList
      }
      existNameList.map((it) => it.code).includes(value)
        ? callback(new Error(value + '已经占用'))
        : callback()
    }

    return {
      showDialog: false, // 控制弹窗显示
      permissionList: [], // 权限管理列表数据（扁平）
      formData: {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: '' // 类型
      },
      rules: {
        name: [
          { required: true, message: '权限名称不能为空', trigger: 'blur' },
          { validator: validName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '权限标识不能为空', trigger: 'blur' },
          { validator: validCode, trigger: 'blur' }
        ]
      },
      edit: false // 是否是编辑操作
    }
  },
  methods: {
    // dialog 关闭事件
    btnCancel() {
      this.$refs.perForm.resetFields()
      this.formData = this.$options.data()
    },

    // 点击取消按钮 -> 回调函数
    CancelFn() {
      this.showDialog = false
    },

    // 点击确定按钮 -> 回调函数
    enterFn() {
      this.$refs.perForm.validate((valid) => {
        if (valid) {
          if (this.edit) {
            this.$emit('editPermissionEv', this.userId, { ...this.formData })
            this.showDialog = false
          } else {
            this.$emit('addPermissionEv', { ...this.formData })
            this.showDialog = false
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
